// pages/my/play/play.js
const api = require('../../api/index.js')
let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cardName: "",
        cardPrice: "",
        cardType: 0,
        Loing: false
    },
    getPhoneNumber: function(e) {

        let sessionKeyS = wx.getStorageSync('session_key').session_key;
        console.log(sessionKeyS);
        if (sessionKeyS == '' || sessionKeyS == undefined) {
            app.Logins();
        }
        let invite_id = wx.getStorageSync('invite_id');
        let data = {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            sessionKey: sessionKeyS,
            invite_id: invite_id
        }
        console.log(data);
        api.getPhoneNumber(data, (res) => {
            if (res.data.code == 50003) {
                this.setData({
                    Loing: true
                })
                wx.showToast({
                    title: '亲，登录需要授权哦~',
                    icon: 'none',
                    duration: 2000
                })
            } else {
                this.setData({
                    userData: res.data.result,
                    Loing: false
                })
                app.globalData.Refresh = true;
                wx.setStorageSync('token', {
                    "token": res.data.result.token,
                    "user_mobile": res.data.result.user_mobile
                });
                console.log(wx.getStorageSync('token'))
            }


        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options, "-----------");
        let cardName = "",
            cardPrice = "";
        switch (options.cardType) {
            case "1":
                cardName = "少儿运动畅享卡";
                cardPrice = 139;
                break;
            case "2":
                cardName = "精英运动臻享卡";
                cardPrice = 499;
                break;
            case "3":
                cardName = "精英运动尊享卡";
                cardPrice = 699;
                break;
            default:
                break;
        }
        this.setData({
            cardName: cardName,
            cardPrice: cardPrice,
            cardType: options.cardType
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    goPlay: function() {
        let openid = wx.getStorageSync('openid').openid;
        let data = {
            id: this.data.cardType,
            openid_wechat: openid
        }
        console.log(openid, 'openid111111111111');
        api.PayCard(data, (res) => {
            console.log(res, '这是一个我要看的数据')
            let playData = res.data.result;
            if(50001 === res.data.code){
            // if (true) {
                // wx.switchTab({
                //     url: `/pages/my/my/my`,
                // })
                this.setData({
                    Loing: true
                })
                wx.showToast({
                    title: '亲，登录需要授权哦~',
                    icon: 'none',
                    duration: 2000
                })
                return false;
            }
            wx.requestPayment({
                'timeStamp': playData.timeStamp,
                'nonceStr': playData.nonceStr,
                'package': playData.package,
                'signType': playData.signType,
                'paySign': playData.paySign,
                'success': function(res) {
                    console.log('交钱成功', res, "准备跳走")
                    wx.redirectTo({
                        url: `/pages/vipCard/index`,
                    })
                },
                'fail': function(res) {
                    console.log(res, '支付失败')
                    wx.showToast({
                        title: '支付失败',
                        icon: 'none'
                    })
                }
            })
        })
    }
})